﻿//using System;
//using System.Text;
//using Microsoft.CodeAnalysis;
//using Microsoft.CodeAnalysis.CSharp;
//using Microsoft.CodeAnalysis.CSharp.Syntax;
//using Microsoft.CodeAnalysis.Text;
//using UIOC.CodeAnalysis.Extensions;
//using UIOC.CodeAnalysis.Helpers;

//namespace UIOC.Observable.CodeAnalysis.Generators;

//internal static class AutowiredGenerator {

//    private static void LogInfo(string message) {
//        SourceOutputHelper.LogInfoToSourceGenLog(DiagnosticSeverity.Info, $"[{nameof(AutowiredGenerator)}] {message}");
//    }

//    public static void GenerateAutowired(
//        GeneratorExecutionContext context,
//        Compilation compilation,
//        UIOCObservableReceiver receiver
//    ) {
//        var autowiredAttributeSymbol = compilation.GetTypeByMetadataName(UIOC.CodeAnalysis.AttributeFullNames.Autowired);
//        if (autowiredAttributeSymbol == null) {
//            return;
//        }

//        LogInfo($"[Assembly({compilation.Assembly.Name})] {nameof(GenerateAutowired)} ...");

//        foreach (var classDeclarationSyntax in receiver.CandidateClasses) {
//            var model = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree);
//            var classSymbol = model.GetDeclaredSymbol(classDeclarationSyntax);
//            if (classSymbol != null) {
//                var (compilationUnitSyntax, namespaceName) = GenerateAutowiredCompilationUnit(
//                    classSymbol,
//                    autowiredAttributeSymbol
//                );
//                if (compilationUnitSyntax == null) {
//                    continue;
//                }
//                var outputSource = SourceText.From(compilationUnitSyntax.NormalizeWhitespace().ToFullString(), Encoding.UTF8);
//                var syntaxTree = CSharpSyntaxTree.ParseText(outputSource, context.ParseOptions as CSharpParseOptions);
//                var (fullFilePath, filePath, fileNameOnly) = classDeclarationSyntax.SyntaxTree.GetGeneratedSourceFilePath(
//                    context.Compilation.Assembly.Name,
//                    namespaceName,
//                    nameof(AutowiredGenerator),
//                    hintName: classSymbol.GetShortName().Replace("<", "(").Replace(">", ")")
//                );
//                syntaxTree = syntaxTree.WithFilePath(fullFilePath);
//                try {
//                    context.AddSource(filePath.Replace(".g.cs", string.Empty).Replace("/", "__").Replace('.', '_') + ".g.cs", outputSource);
//                    LogInfo($"[File({fileNameOnly})] ...\n{string.Join("\n", outputSource.Lines)}");
//                    SourceOutputHelper.OutputSourceToFile(fullFilePath, outputSource.ToString);
//                } catch (ArgumentException) {
//                }
//            }
//        }
//    }

//    private static (CompilationUnitSyntax CompilationUnitSyntax, string NamespaceName) GenerateAutowiredCompilationUnit(
//        INamedTypeSymbol classSymbol,
//        INamedTypeSymbol autowiredAttributeSymbol
//    ) {
//        return (null, null);
//    }
//}
